package org.hagopa.games.moneypool.dblayer;

import java.sql.CallableStatement;
import java.sql.Date;
import java.sql.SQLException;

import java.sql.ResultSet;

import org.hagopa.games.moneypool.dblayer.IDataAdopt;
import org.hagopa.games.moneypool.dblayer.Connector.DBConnector;

public class GameGeneralInfoAdopt implements IDataAdopt{
	private long game_id = 0;
	private int max_players = 0;
	private boolean waitFlag = false;
	private int game_type = 0; //by default 0 - public
	private int template_id = 0;
	private int board_sz = 0;
	private long start_gm_sum = 0;
	private int start_payment = 0;
	private Date start_date = null;
	private String dbquery = "";
	
	public GameGeneralInfoAdopt(long gmId){
		this.game_id = gmId;
		this.dbquery = "{call mp_get_game_info(?);}";
	}
	
	public boolean requestDB()
	{
		ResultSet rs = null;
        //Statement stmt = null;
        try {
       	 
            CallableStatement cs = DBConnector.getConn().prepareCall(this.dbquery);
       	    				//TODO: Should implement passing parameters to stored procedure
       	    cs.setLong(1, this.game_id);
       	    rs = cs.executeQuery();

       	    this.parseDBresponse(rs);
       	    rs.close();
       	    //stmt.close();
      	    		
       	} catch (SQLException e) {
      	    			// TODO Auto-generated catch block
        		e.printStackTrace();
        		return false;
      	}
        return true;
	}
	public String getQuery() {
		// TODO Auto-generated method stub
		return this.dbquery;
	}

	public boolean parseDBresponse(ResultSet rs) {
		try {
			while(rs.next())
			{
				this.max_players = rs.getInt("max_attendies");
				this.game_type = rs.getInt("is_private");
				int tmp = rs.getInt("is_wait");
				if( tmp > 0)
				{
					this.waitFlag = true;
				}
				else
				{
					this.waitFlag = false;
				}
				
				//this.template_id = rs.getInt("template_id");
				this.board_sz = rs.getInt("board_sz");
				this.start_gm_sum = rs.getLong("init_amount");
				this.start_payment = rs.getInt("start_payment");
				this.start_date = rs.getDate("start_date");
				System.out.println("[GameGeneralInfoAdop](parseDBresponse) - Data recieved for Game id:" + game_id);
				System.out.println("[GameGeneralInfoAdop](parseDBresponse) - max_attendies = "+this.max_players);
				System.out.println("[GameGeneralInfoAdop](parseDBresponse) - is_private = " + this.game_type);
				System.out.println("[GameGeneralInfoAdop](parseDBresponse) - board_sz = "+this.board_sz);
				System.out.println("[GameGeneralInfoAdop](parseDBresponse) - init_amount = "+this.start_gm_sum);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public long getGame_id() {
		return game_id;
	}

	public void setGame_id(int game_id) {
		this.game_id = game_id;
	}

	public int getBoard_sz() {
		return board_sz;
	}

	public int getGame_type() {
		return game_type;
	}

	public int getMax_players() {
		return max_players;
	}

	public Date getStart_date() {
		return start_date;
	}

	public long getStart_gm_sum() {
		return start_gm_sum;
	}

	public int getTemplate_id() {
		return template_id;
	}

	public boolean isWaitFlag() {
		return waitFlag;
	}

	public int getStart_payment() {
		return start_payment;
	}

	public String getQueryType() {
		// TODO Auto-generated method stub
		return "proc";
	}

}

// * Maximum players
//* flag wait for all players (IGF: Is it possible to start game when not all players connected ?)
//* game type (public / private)
//* Template Id
//*  Board size
//* Start Game sum (total in bank)
//* sum with it players start game (IGF: ???)
//* start-payment - player receives every time he passes start (IGF:??? is it game property ??)
// * Start timestamp - when game starts if defined